import styles from './styles.less';
import { Pagination, Table, Space, Tag, Modal } from 'antd';
import { useCallback, useEffect, useState } from 'react';
import { ExclamationCircleFilled } from '@ant-design/icons';
import Opera from './Opera';

import { connect, useAccess } from 'umi';

const DataTable = (props) => {
  const access = useAccess();
  const { dispatch, dataTable, loading, submitting, unlimitting } = props;
  const { salersSetting, setDataForm, tablePayload, setTablePayload } = props.data;
  const [selectedRowKeys, setSelectedRowKeys] = useState([]);

  /* 获取列表 */
  const fetchList = useCallback((payload) => {
    if (!payload.param.campainId) {
      dispatch({
        type: 'POperationCampainSalersSetting/chgDataTable',
        payload: { total: 0, list: [] },
      });
    } else {
      dispatch({
        type: 'POperationCampainSalersSetting/fetchList',
        payload: payload,
      });
    }
  }, [dispatch]);

  /* 查询监听 */
  useEffect(() => {
    fetchList(tablePayload);
    fetchList({
      ...tablePayload, param: {
        ...tablePayload.param,
        campainId: salersSetting ? salersSetting.data.id : undefined,
      },
    });
  }, [fetchList, tablePayload, salersSetting]);

  /* 删除 */
  function del(e, record) {
    e.preventDefault();
    if (!submitting) {
      Modal.confirm({
        title: '提示',
        icon: <ExclamationCircleFilled />,
        content: '是否确定删除数据？',
        onOk() {
          dispatch({
            type: 'POperationCampainSalersSetting/del',
            payload: [record.id],
            callback: () => {
              setTablePayload({ ...tablePayload, pageNum: 1 });
              setSelectedRowKeys([]);
            },
          });
        },
      });
    }
    return false;
  }

  /* 下载推广码 */
  function qrcode(e, record) {
    e.preventDefault();
    if (!unlimitting) {
      Modal.confirm({
        title: '提示',
        icon: <ExclamationCircleFilled />,
        content: '是否确定下载推广码？',
        onOk() {
          dispatch({
            type: 'POperationCampainSalersSetting/unlimit',
            payload: record.id,
            callback: () => {
              setSelectedRowKeys([]);
            },
          });
        },
      });
    }
    return false;
  }

  const columns = [
    {
      title: '序号',
      width: 70,
      align: 'center',
      render: ({}, {}, index) => (
        <span>
          {(tablePayload.pageNum - 1) * tablePayload.pageSize + index + 1}
        </span>
      ),
    },
    {
      title: '用户名称',
      dataIndex: 'salerName',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '手机号码',
      dataIndex: 'salerMobile',
      align: 'center',
      ellipsis: true,
    },
    {
      title: '激活状态',
      dataIndex: 'active',
      align: 'center',
      render: (_, record) => {
        return (
          <Tag color={record.active === 1 ? 'geekblue' : 'volcano'}>
            {record.active === 1 ? '已激活' : '待激活'}
          </Tag>
        );
      },
    },
    {
      title: '当前状态',
      dataIndex: 'status',
      align: 'center',
      render: (_, record) => {
        return (
          <Tag color={record.campainSaler.status === 1 ? 'geekblue' : 'volcano'}>
            {record.campainSaler.status === 1 ? '启用' : '停用'}
          </Tag>
        );
      },
    },
    {
      title: '创建时间',
      dataIndex: 'createTime',
      align: 'center',
      render: (_, record) => {
        return (
          <span>
            {record.campainSaler.createTime ? record.campainSaler.createTime : '-'}
          </span>
        );
      },
    },
    {
      title: '操作',
      dataIndex: 'option',
      align: 'center',
      ellipsis: true,
      width: 140,
      render: (_, record) => {
        return (
          <Space size="small">
            {(access?.campain_salerLink_del) && (
              <a onClick={e => del(e, record)}>删除</a>
            )}
            <a onClick={e => qrcode(e, record)}>推广码</a>
          </Space>
        );
      },
    },
  ];

  return (
    <>
      <div className={styles.myDataTable}>
        <Opera data={{ setDataForm, selectedRowKeys, setSelectedRowKeys, tablePayload, setTablePayload }} />
        <Table
          rowSelection={{
            selectedRowKeys,
            onChange: (newSelectedRowKeys) => setSelectedRowKeys(newSelectedRowKeys),
          }}
          className={styles.myTable} bordered={false} size={'middle'} columns={columns}
          dataSource={dataTable.list} rowKey={'id'}
          pagination={false} scroll={{ y: '100%' }} loading={loading}
        />
      </div>
      <Pagination
        pageSize={tablePayload.pageSize} current={tablePayload.pageNum} total={dataTable.total}
        className={styles.myPagination} size={'default'} hideOnSinglePage={false}
        showTotal={(total) => {
          return `共 ${total} 条记录`;
        }}
        pageSizeOptions={[10, 30, 60, 120]}
        showSizeChanger showQuickJumper={true}
        onChange={(page, pageSize) => {
          setTablePayload({ ...tablePayload, pageNum: page, pageSize: pageSize });
        }}
      />
    </>

  );
};

export default connect(({ loading, POperationCampainSalersSetting }) => {
  return {
    loading: loading.effects['POperationCampainSalersSetting/fetchList'],
    submitting: loading.effects['POperationCampainSalersSetting/del'],
    unlimitting: loading.effects['POperationCampainSalersSetting/unlimit'],
    dataTable: POperationCampainSalersSetting.dataTable,
  };
})(DataTable);
